﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cfg;

namespace DocuServe.Data.Context
{
    internal class SessionContextManagerFactory
    {
        public SessionContextManagerFactory(Configuration configuration)
        {
            this.Configuration = configuration;
        }

        private Configuration Configuration { get; set; }

        public ISessionContextManager Create()
        {
            if (!this.Configuration.Properties.ContainsKey("current_session_context_class"))
                throw new InvalidOperationException("The current_session_context_class property is not defined in the nhibernate configuration.");

            string value = this.Configuration.Properties["current_session_context_class"];
            switch (value)
            {
                case "managed_web":
                    return new ManagedWebSessionContextManager();
                case "call":
                    return new CallSessionContextManager();
                case "thread_static":
                    return new ThreadStaticSessionContextManager();
                case "web":
                    return new WebSessionContextManager();
                default:
                    throw new InvalidOperationException(string.Format("{0} value is not a valid ICurrentSessionContext.", value));
            }
        }
    }
}
